
// C#
[WebService(Namespace="http://www.llblgen.com/examples")]
public class CustomerService : System.Web.Services.WebService
{
[WebMethod]
public CustomerEntity GetCustomer(string customerID)
{
CustomerEntity toReturn = new CustomerEntity(customerID);
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
adapter.FetchEntity(toReturn);
return toReturn;
}
}
[WebMethod]
public EntityCollection GetCustomers()
{
EntityCollection customers = new EntityCollection(new CustomerEntityFactory());
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
adapter.FetchEntityCollection(customers, null);
return customers;
}
}
[WebMethod]
public bool SaveCustomer(CustomerEntity toSave)
{
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
return adapter.SaveEntity(toSave);
}
}
}' VB.NET <WebService(Namespace="http://www.llblgen.com/examples")> _ Public Class CustomerService Inherits System.Web.Services.WebService <WebMethod> _ Public Function GetCustomer(customerID As String) As CustomerEntity Dim toReturn As New CustomerEntity(customerID) Dim adapter As New DataAccessAdapter() Try adapter.FetchEntity(toReturn) Return toReturn Finally adapter.Dispose() End Try End Function <WebMethod> _ Public Function GetCustomers() As EntityCollection Dim customers As New EntityCollection(New CustomerEntityFactory()) Dim adapter As New DataAccessAdapter() Try adapter.FetchEntityCollection(customers, Nothing) Return customers Finally adapter.Dispose() End Try End Function <WebMethod> _ Public Function SaveCustomer(toSave As CustomerEntity) As Boolean Dim adapter As New DataAccessAdapter() Try Return adapter.SaveEntity(toSave) Finally adapter.Dispose() End Try End Function End Class
Note: |
When sending entities over the wire using WCF, the 'IsNew' flag is not passed along as it is determinable on the service side for new entities. If you use the trick where you set the IsNew flag manually on an entity and then send the entity over the wire to the service, the IsNew flag is set to false at the service during deserialization, so you have to set it back to true in that special case scenario. |
// C#
[ServiceContract]
[ServiceKnownType(typeof(CustomerEntity))]
[ServiceKnownType(typeof(EntityCollection))]
public interface IWCFExample
{
[OperationContract]
IEntity2 GetCustomer(string customerID);
[OperationContract]
IEntityCollection2 GetCustomers();
}
' VB.NET
<ServiceContract(), _
ServiceKnownType(GetType(CustomerEntity)), _
ServiceKnownType(GetType(EntityCollection))> _
Public Interface IWCFExample
<OperationContract()> _
Function GetCustomer(customerID As String) As IEntity2
<OperationContract(&)gt; _
Function GetCustomers() As IEntityCollection2
End Interface
// C#
// class to implement the service logic
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class WCFExampleService : IWCFExample
{
public IEntity2 GetCustomer(string customerID)
{
CustomerEntity toReturn = new CustomerEntity(customerID);
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
adapter.FetchEntity(toReturn);
}
return toReturn;
}
public IEntityCollection2 GetCustomers()
{
EntityCollection toReturn = new EntityCollection(new CustomerEntityFactory());
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
adapter.FetchEntityCollection(toReturn, null);
}
return toReturn;
}
}
// class to actually run the service:
public class WCFExampleServerHost
{
public WCFExampleServerHost()
{
WCFExampleService server = new WCFExampleService();
ServiceHost host = new ServiceHost(server);
host.Open();
}
}
' VB.NET
' class to implement the service logic
<ServiceBehavior(InstanceContextMode := InstanceContextMode.Single)> _
Public Class WCFExampleService
Implements IWCFExample
Public Function GetCustomer(customerID As string) As IEntity2 Implements IWCFExample.GetCustomer
Dim toReturn As new CustomerEntity(customerID)
Using adapter As New DataAccessAdapter()
adapter.FetchEntity(toReturn)
End Using
Return toReturn
End Function
Public Function GetCustomers() As IEntityCollection2 Implements IWCFExample.GetCustomers
Dim toReturn As New EntityCollection(New CustomerEntityFactory())
Using adapter As New DataAccessAdapter()
adapter.FetchEntityCollection(toReturn, Nothing)
End Using
Return toReturn
End Function
End Class
' class to actually run the service:
Public Class WCFExampleServerHost
Public Sub New()
Dim server As New WCFExampleService()
Dim host As New ServiceHost(server)
host.Open()
End Sub
End Class
// C#
ChannelFactory<IWCFExample> channelFactory =
new ChannelFactory<IWCFExample>("WCFExampleServer");
IWCFExample server = channelFactory.CreateChannel();
// Fetch an entity
IEntity2 c = server.GetCustomer("CHOPS");
// Fetch a collection
IEntityCollection2 customers = serverTest.GetCustomers();
' VB.NET
Dim channelFactory As New ChannelFactory(Of IWCFExample)("WCFExampleServer")
Dim server As IWCFExample = channelFactory.CreateChannel()
' Fetch an entity
Dim c As IEntity2 = server.GetCustomer("CHOPS")
' Fetch a collection
Dim customers As IEntityCollection2 = serverTest.GetCustomers()
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="RemoteConfig"
closeTimeout="infinite"
openTimeout="infinite"
sendTimeout="infinite"
receiveTimeout="infinite"
maxBufferSize="65536000"
maxReceivedMessageSize="65536000" />
</netTcpBinding>
</bindings>
<services>
<service name="Service.WCFExampleServer">
<endpoint address="" binding="netTcpBinding" name="WCFExampleServer"
bindingConfiguration="RemoteConfig"
contract="Interfaces.IWCFExample" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:6543/WCFExampleServer" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="RemoteConfig"
closeTimeout="infinite"
openTimeout="infinite"
sendTimeout="infinite"
receiveTimeout="infinite"
maxBufferSize="65536000"
maxReceivedMessageSize="65536000" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:6543/WCFExampleServer"
name="WCFServer" binding="netTcpBinding"
bindingConfiguration="RemoteConfig"
contract="Interfaces.IWCFExample" />
</client>
</system.serviceModel>